﻿// BatteryViewPage.cpp : implementation file
//
#include "stdafx.h"
#include "BtsClient.h"
#include "BatteryViewPage.h"
#include "BttmView.h"
#include "Manager.h"
#include "UnitItem.h"

#include "MainFrm.h"
#include "..\COMM\constant.h"
#include "JumpStep.h"
#include "StartChannel.h"
#include "CommHandler.h"
#include "ChannelInfo.h"
#include "../client_struct.h"
#include "SettingMgr.h"
#include "ChannelLog.h"
#include "StepEditExport.h"
//#include "LogFile.h"
#include "StepFile.h"
#include "ColorMgr.h"

static BOOL g_enterInput = FALSE;
IMPLEMENT_DYNAMIC(CBatteryViewPage, CPropertyPage)

CBatteryViewPage::CBatteryViewPage()
: CPropertyPage(CBatteryViewPage::IDD)
{
	m_pBatteryControl = new CBatteryControl();
	LOG_POINT(m_pBatteryControl);
	//安装一个8行16列的矩阵空间,添加的电池不要超过这个范围,电池矩形宽120,高160
	m_pBatteryControl->Initialize(this,0,0,
		0,0,TRUE);	
	
	m_pCurrentSelect = NULL;
	m_pSelectedBattery = NULL;
//	m_pDASelectBattery = NULL;

	m_fontText.CreateFont(16, 0, 0, 0, 400, FALSE, FALSE, 0, ANSI_CHARSET, 
		OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, 
		DEFAULT_PITCH | FF_SWISS, _T("Arial"));

	m_nsbWidth = 17;
	m_nYUnitPixel = 5; 
	m_isFist = 0;
	m_pLogdlg = NULL;
	m_pChlInfoDlg = NULL;
}

CBatteryViewPage::~CBatteryViewPage()
{
	if (m_pBatteryControl)
	{
		delete m_pBatteryControl;
		m_pBatteryControl = NULL;
	}
}

void CBatteryViewPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CBatteryViewPage, CPropertyPage)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_WM_TIMER()
	ON_WM_HSCROLL()
	ON_WM_VSCROLL()
	ON_WM_RBUTTONUP()
	ON_WM_SIZE()
	ON_WM_MOUSEHOVER()
	ON_WM_SHOWWINDOW()
	ON_WM_ERASEBKGND()
	ON_WM_NCCALCSIZE()

	ON_WM_MOUSEWHEEL()
	ON_MESSAGE(CBtsControlApp::USER_MSG_UPDATE_BATTVIEW,&CBatteryViewPage::OnUserMsgUpdateBattView)
	ON_MESSAGE(CBtsControlApp::USER_MSG_UPDATE_BATTVIEW2,&CBatteryViewPage::OnUserMsgUpdateBattView2)
	ON_REGISTERED_MESSAGE(CMenuEx::sm_uMsgMenuCommand, OnMenuCommand)

	ON_WM_DESTROY()
	ON_WM_SETCURSOR()
	ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()


// CBatteryViewPage message handlers

void CBatteryViewPage::OnPaint()
{
	//	TRACE("画图开始。。。。");
	CPaintDC dc(this);
	//
	HGDIOBJ hOldFont = dc.SelectObject(m_fontText);

	CRect rcClient;

	GetClientRect( rcClient );

	CDC memDC;
	CBitmap memBmp;
	memBmp.CreateCompatibleBitmap(&dc,rcClient.Width() ,rcClient.Height());
	memDC.CreateCompatibleDC(NULL);
	memDC.SelectObject(&memBmp);
	HGDIOBJ hOldFontMem = memDC.SelectObject(m_fontText);
	
	memDC.FillSolidRect(rcClient, g_Color->GetCtrlColor("SystemColor", "MainChlBack"));
	m_pBatteryControl->DrawAllBattery(&memDC, rcClient);
	m_pBatteryControl->DrawAllBatteryContent(&memDC, rcClient);//画电池内容


	dc.BitBlt( rcClient.left,rcClient.top, rcClient.Width(),rcClient.Height() , 
		&memDC ,0,0,SRCCOPY );
	
	//
	memDC.SelectObject(hOldFontMem);

	memBmp.DeleteObject();
	memDC.DeleteDC();

	//
	dc.SelectObject(hOldFont);
}

BOOL CBatteryViewPage::OnInitDialog()
{
	//设置画电池的区域
	CPropertyPage::OnInitDialog();
	CRect rcClient(0,0,0,0);
	GetClientRect(rcClient);
	rcClient.DeflateRect( 0,0,20,20);
	m_pBatteryControl->SetWndArea( rcClient );

	CRect ClientRect;
	GetClientRect(ClientRect);

	CPaintDC dc(this); 
	//将背景填充为灰色   
	CBrush br;
	br.CreateSolidBrush(RGB(196,196,196) );
	dc.FillRect(&ClientRect,&br); 

	//2秒钟更新一次实时数据。
	string sInterval = g_Setting->GetUserFileKeyValue("BatteryShowMode", "BattUpdateInterval");
	int nInterval =  FromString<int>(sInterval);
	if (nInterval < 500)
	{
		nInterval = 500;
	}

	SetTimer(TIMER_UPDATERT, nInterval,NULL);
	InitScrollBar();
	//SetColorStyle();
	g_enterInput = TRUE;
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CBatteryViewPage::OnLButtonDown(UINT nFlags, CPoint point)
{
	SetCapture();
	m_pBatteryControl->LButtonDown(nFlags,point);

	CPropertyPage::OnLButtonDown(nFlags, point);
}


void CBatteryViewPage::OnMouseMove(UINT nFlags, CPoint point)
{
	if (nFlags == MK_LBUTTON)
	{
		m_bMouseDraging = TRUE;
		//theApp.m_bAuxChlDraging = FALSE;
	}
	else
	{
		m_bMouseDraging = FALSE;
		m_vecDragBattery.clear();
	}
	// 
	TRACKMOUSEEVENT tmo;
	tmo.cbSize = sizeof (TRACKMOUSEEVENT);
	tmo.dwFlags = TME_HOVER;
	tmo.dwHoverTime = 10;
	tmo.hwndTrack = GetSafeHwnd();
	TrackMouseEvent( &tmo );
	CPropertyPage::OnMouseMove(nFlags, point);

}

void CBatteryViewPage::OnLButtonUp(UINT nFlags, CPoint point)
{
	ReleaseCapture();
	SetActiveWindow();

	CMainFrame *pMainFrame = (CMainFrame *)GetParentFrame();
	m_pBatteryControl->LButtonUp(nFlags,point);
	CBatteryItem *pMainBat = m_pBatteryControl->GetBatteryIndexByPoint(point);
	SetFocus();	//为了 本类的PreTranslateMessage接收keydown
	
	if (pMainFrame && pMainBat)
	{
		pMainFrame->PostMessage(CBtsControlApp::USER_MSG_CLICK_CHL, pMainBat->GetUnitID(), pMainBat->GetChlID());//通知弹出的查询窗口改变设备号/通道号
		FreshChlLog(pMainBat->GetUnitID(), pMainBat->GetChlID());
		FreshChlInfo(pMainBat->GetUnitID(), pMainBat->GetChlID());
	}

	if (m_vecDragBattery.size() == 0)
	{
		return;
	}

	CPropertyPage::OnLButtonUp( nFlags, point );
}

void CBatteryViewPage::OnTimer(UINT_PTR nIDEvent)
{
	//CRect cr ; 
	//GetClientRect( &cr );//

	//设置并显示滚动条
	//改变电池大小后需要滚动条时
	//无此函数，第一次改变电池大小后，滚动条滑块无滑动空间
	//ChangeScrollBar();

	//
	if (TIMER_UPDATERT == nIDEvent)
	{		
		UpdateRTDisp();
	}
	else if (1 == nIDEvent)
	{

		CMainFrame *pMainFrame = (CMainFrame *)GetParentFrame();
		//pMainFrame->ScanInput(g_inputStr);

		g_inputStr = "";

		KillTimer(1);
		g_enterInput = true;
	}
	CPropertyPage::OnTimer(nIDEvent);
}

//更新RT数据显示。
//
//cg  2010-04-22
//
void CBatteryViewPage::UpdateRTDisp(void)
{
	m_pBatteryControl->UpdateRTDisp();
	m_pBatteryControl->RedrawAllBattery();
}

void CBatteryViewPage::OnRButtonUp(UINT nFlags, CPoint point)
{
	if (!m_pBatteryControl)
	{
		return;
	}
	//
	m_pCurrentSelect = NULL;
	CBatteryItem* pCurrentSelect = NULL;
	int nCount = m_pBatteryControl->RButtonUp(point, &pCurrentSelect);
//#ifndef _DEBUG
	if (nCount <= 0 || !pCurrentSelect->GetBatteryShow())
	{
		//没有有效电池被选中。
		TRACE("CBatteryViewPage::OnRButtonUp() 没有有效电池被选中\r\n");

		return;
	}
	m_pCurrentSelect = pCurrentSelect;
//#endif
	PriviInfo pri = g_BtsMgr.GetActiveDevPrivi();
	if (pri.m_nLeftDays <= 0)
	{
		if (pri.m_nPrivi == 0)
		{
			AfxMessageBox(_T("试用期已经结束，请联系供应商."));
			return ;
		} 
		else if (pri.m_nPrivi == 1)
		{
			AfxMessageBox(_T("第一期款使用期已经结束，请联系供应商."));
			return ;
		} 
		else if (pri.m_nPrivi == 2)
		{
			AfxMessageBox(_T("第二期款使用期已经结束，请联系供应商."));
			return ;
		} 
	} 
	
	CPoint pt;
	GetCursorPos(&pt);
	m_pBatteryControl->PopRBMenu(pt, GetSafeHwnd());
	
	//
	CPropertyPage::OnRButtonUp(nFlags, point);
}

void CBatteryViewPage::OnSize(UINT nType, int cx, int cy)
{
	CPropertyPage::OnSize(nType, cx, cy);

	//设置并显示滚动条
	ChangeScrollBar();
	//CRect rc;
	//GetClientRect(rc);
	//m_pBatteryControl->SetWndArea(rc);
	//SetDocSize(m_pBatteryControl->GetDocSize());
}

void CBatteryViewPage::OnMouseHover(UINT nFlags, CPoint point)
{
	m_pBatteryControl->MouseMove(nFlags,point);
	CPropertyPage::OnMouseHover(nFlags, point);
}

void CBatteryViewPage::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CPropertyPage::OnShowWindow(bShow, nStatus);
	CBttmView* btView = (CBttmView *)GetParent()->GetParent();
	CString strTitle;
	strTitle.LoadString(IDS_TEXT_Normal);
	if (bShow)
	{
		btView->SetText(strTitle);
	}
}

BOOL CBatteryViewPage::OnEraseBkgnd(CDC* pDC)
{
	//	return CPropertyPage::OnEraseBkgnd(pDC);
	return TRUE;
}

//右键菜单,全选
void CBatteryViewPage::OnMSelectAll()
{

	m_pBatteryControl->SelectAllBattery();
	//
	TRACE("CBatteryViewPage::OnMSelectAll() ok\r\n");
}

bool CBatteryViewPage::CheckStepTheOne( const CGetChlInfoRsp& rsp, const char* szGuiStep )
{
	for (int i = 0; i < rsp.m_wChlCount; i++)
	{
		if (strcmp(szGuiStep, rsp.m_infoList[i].m_chlInfo.guidStep) != 0)
		{
			return false;
		}
	}
	return true;
}

//右键菜单,跳转
void CBatteryViewPage::OnMJump()
{	
	BOOL bSuccess = TRUE;
	//获取所选通道信息
	stChlTestInfo chlTest;
	memset(&chlTest, 0, sizeof(chlTest));
	vector<UnitChlList> vSel = m_pBatteryControl->GetSelectChl();
	for (size_t i = 0; i < vSel.size(); i++)
	{
		CGetChlInfoRsp rsp;
		if (0 == g_CommHandler.GetClientNet().GetChlInfo(vSel[i], rsp) && rsp.m_wChlCount > 0)
		{
			memcpy(&chlTest , &rsp.m_infoList[0].m_chlInfo, sizeof(chlTest));//第一个作为初始化
			
			if (strlen(chlTest.guidStep) == 0)
			{
				AfxMessageBox(_T("没有通道信息"));
				bSuccess = FALSE;
				theApp.m_OpLog.OnControlChannel(CLS_MID_CMD_JUMP_CHL_TEST, vSel[i], bSuccess);
				break;
			} 
			
			//检查工步是否同一个
			if (!CheckStepTheOne(rsp, chlTest.guidStep))
			{
				AfxMessageBox(_T("所选通道不是同一个工步文件，无法一次跳转"));
				bSuccess = FALSE;
				theApp.m_OpLog.OnControlChannel(CLS_MID_CMD_JUMP_CHL_TEST, vSel[i], bSuccess);
				break;
			}
		}
		else
		{
			AfxMessageBox(_T("通讯失败"));
			bSuccess = FALSE;
			theApp.m_OpLog.OnControlChannel(CLS_MID_CMD_JUMP_CHL_TEST, vSel[i], bSuccess);
			break;
		}
	}
	//显示工步数据，让用户选择跳转
	if (bSuccess)
	{
		CJumpStep dlg;
		dlg.m_chlInfo = chlTest;
		if (IDOK == dlg.DoModal())
		{
			for (size_t i = 0; i < vSel.size(); i++)
			{
				int nRet = g_CommHandler.GetClientNet().JumpStep(vSel[i], 
					chlTest.guidStep, dlg.m_nToStepNo);

				theApp.m_OpLog.OnControlChannel(CLS_MID_CMD_JUMP_CHL_TEST, vSel[i], nRet);
			}
		}
	}

}

//右键菜单,启动
void CBatteryViewPage::OnMenuStart(void)
{
	UnitChlList unitChls = GetSelectChls();
	CUnitItem* pUnit = g_BtsMgr.GetActiveDev();
	if (unitChls.vChls.size() == 0 || !pUnit)
		return;	

	//获取设备量程,取最小电流通道的
	vector<ShortDouble> vRange = pUnit->GetDcPara(unitChls.vChls[0]);
	for (int k = 1; k < unitChls.vChls.size(); k++)
	{
		vector<ShortDouble> vRange2 = pUnit->GetDcPara(unitChls.vChls[k]);
		if (vRange.size() >=4 && vRange2.size() >=4 && vRange[2].dValue - vRange[3].dValue > vRange2[2].dValue - vRange2[3].dValue)
		{
			vRange = vRange2;
		}
	}

	CStepEidtDll stepsDll;
	if (vRange.size() >= 4)
	{
		stepsDll.SetStepRange(vRange[1].dValue,vRange[0].dValue,vRange[3].dValue,vRange[2].dValue);
	} 

	CString strFile,strPrjName;
	int nStart;
	TRACE(_T("\n OnMenuStart %d"), GetTickCount());
	int nRet = stepsDll.ShowStartDlg(pUnit->GetUnitNo(), strFile, nStart, strPrjName);
	if (0 == nRet)
	{
		vector<UnitChlList> v;
		v.push_back(unitChls);
		int ret = g_CommHandler.GetClientNet().SendDownStepAndStart2(nStart,v,strFile,strPrjName);
		//if (ret == 0)
		//{
		//	
		//}
	}
	//}


}
//通道复制
void CBatteryViewPage::OnMenuCopy()
{
	bool bFail = false;

	UnitChlList devChls;
	devChls.unit_no = m_pCurrentSelect->GetUnitID();
	devChls.vChls.push_back(m_pCurrentSelect->GetChlID());
	if (0 == g_CommHandler.GetClientNet().GetChlInfo(devChls, m_rsp))
	{
		string sGuidStep = m_rsp.m_infoList[0].m_chlInfo.guidStep;
		if (sGuidStep.empty())
		{
			AfxMessageBox(_T("复制失败！"));
			bFail = true;
		}
	}
	else
	{
		AfxMessageBox(_T("复制失败！"));
		bFail = true;
	}

	theApp.m_OpLog.WriteSysOperateLog("复制工步", 
		ToString<short>(m_pCurrentSelect->GetUnitID())+"-"+ToString<short>(devChls.vChls[0]), 
		bFail?"失败":"成功");
}
//复制启动
void CBatteryViewPage::OnMenuCopyStart()
{
	string stepName = m_rsp.m_infoList[0].m_chlInfo.guidStep;
	stepName += ".stp";
	stepName = GetSetting()->GetSysPath()+"temp_stp\\" + stepName;

	string sArg;
	vector< CUnitItem* > vUnitItem = m_pBatteryControl->GetShowUnit();
	vector<UnitChlList> v;
	for (size_t i = 0; i < vUnitItem.size(); i++)
	{
		short nUnitNo = vUnitItem[i]->GetUnitNo();
		UnitChlList devChls;
		devChls.unit_no = nUnitNo;
		vector< CBatteryItem* > vBat = vUnitItem[i]->GetAllBattery();
		vector< CBatteryItem* >::iterator it = vBat.begin();
		while (it != vBat.end())
		{
			if ((*it)->GetStatus())
			{
				devChls.vChls.push_back((*it)->GetChlID());
				sArg += ToString<short>(nUnitNo)+"-"+ToString<short>((*it)->GetChlID())+";";
			}
			it++;
		}

		v.push_back(devChls);
		theApp.m_OpLog.WriteSysOperateLog("粘贴启动", sArg, "*");
		
		sArg = "";
	}

	CString newfile = CA2W(stepName.c_str());
	g_CommHandler.GetClientNet().SendDownStepAndStart2(1, v, newfile);
}

void CBatteryViewPage::FreshChlInfo( int nDevID,int nChlID )
{
	if (m_pChlInfoDlg && m_pChlInfoDlg->IsWindowVisible() && 
		m_pChlInfoDlg->GetUnitChlNo() != g_BtsMgr.GetUnitChlNo(nDevID,nChlID))
	{
		CGetChlInfoRsp rsp;

		UnitChlList devChls;
		devChls.unit_no = nDevID;
		devChls.vChls.push_back(nChlID);
		string sArg = ToString<short>(nDevID) + "-" + ToString<short>(nChlID);
		int nRet = g_CommHandler.GetClientNet().GetChlInfo(devChls, rsp);
		if ( nRet != 0 || 0 != rsp.m_nRspCode|| strlen(rsp.m_infoList[0].m_chlInfo.guidStep) == 0)
		{
			theApp.m_OpLog.WriteSysOperateLog("查看通道信息", sArg, "暂无通道信息");
			return;
		}

		m_pChlInfoDlg->SetDefaultChl(nDevID, nChlID);
		m_pChlInfoDlg->SetChlInfo(rsp.m_infoList[0].m_chlInfo);
		m_pChlInfoDlg->ShowWindow(SW_SHOW);

		theApp.m_OpLog.WriteSysOperateLog("查看通道日志", sArg, "失败");
	}
}

//右键菜单,通道信息
void CBatteryViewPage::OnMInfo()
{
	UnitChlList devChls;
	devChls.unit_no = m_pCurrentSelect->GetUnitID();
	devChls.vChls.push_back(m_pCurrentSelect->GetChlID());
	string sArg = ToString<short>(m_pCurrentSelect->GetUnitID()) + "-" + ToString<short>(m_pCurrentSelect->GetChlID());

	CGetChlInfoRsp rsp;
	if (0 == g_CommHandler.GetClientNet().GetChlInfo(devChls, rsp))
	{
		string sGuidStep = rsp.m_infoList[0].m_chlInfo.guidStep;
		if (sGuidStep.empty())
		{
			theApp.m_OpLog.WriteSysOperateLog(" 查看通道信息", sArg, "暂无通道信息");
			AfxMessageBox(_T("暂无通道信息"));
			return;
		}
		//CChannelInfo dlg;
		//dlg.m_chlInfo = rsp.m_infoList[0].m_chlInfo;
		//dlg.SetDefaultChl(m_pCurrentSelect->GetUnitID(), m_pCurrentSelect->GetChlID());
		//dlg.DoModal();
		if (!m_pChlInfoDlg)
		{
			m_pChlInfoDlg = new CChannelInfo;
			m_pChlInfoDlg->Create(IDD_CHAANNEL_INFO,this);
		}

		m_pChlInfoDlg->SetDefaultChl(m_pCurrentSelect->GetUnitID(), m_pCurrentSelect->GetChlID());
		m_pChlInfoDlg->SetChlInfo(rsp.m_infoList[0].m_chlInfo);
		m_pChlInfoDlg->ShowWindow(SW_SHOW);
	}
	else
	{
		theApp.m_OpLog.WriteSysOperateLog(" 查看通道信息", sArg, "失败");
		AfxMessageBox(_T("获取通道信息失败"));
		return;
	}
}
//右键菜单： 通道日志
void CBatteryViewPage::OnMenuChlLog()
{
	TRACE("CBatteryViewPage::OnMenuChlLog() \r\n");
	CGetChlInfoRsp rsp;
	UnitChlList devChls;
	devChls.unit_no = m_pCurrentSelect->GetUnitID();
	devChls.vChls.push_back(m_pCurrentSelect->GetChlID());
	string sArg = ToString<short>(m_pCurrentSelect->GetUnitID()) + "-" + ToString<short>(m_pCurrentSelect->GetChlID());
	if (0 != g_CommHandler.GetClientNet().GetChlInfo(devChls, rsp))
	{
		AfxMessageBox(_T("获取通道信息失败"));
		theApp.m_OpLog.WriteSysOperateLog("查看通道日志", sArg, "获取通道信息失败");
		return;
	}

	
	if (0 == rsp.m_nRspCode)
	{
		BTS_DATA_CHL chl;
		chl.unit_id = m_pCurrentSelect->GetUnitID();
		chl.chl_id = m_pCurrentSelect->GetChlID();
		chl.btDevType = g_BtsMgr.GetUnitItem(chl.unit_id)->GetDevType();
		stChlLogData stLog;
		if (0 < g_CommHandler.GetDBHandle().GetChlHisLog(stLog, rsp.m_infoList[0].m_chlInfo))	
		{
			theApp.m_OpLog.WriteSysOperateLog(" 查看通道日志", sArg, "成功");

			//CChannelLog dlg;
			//dlg.SetDefaultChl(chl.unit_id,chl.chl_id);
			//dlg.SetChlLog(stLog);
			//dlg.DoModal();
			if (NULL == m_pLogdlg)
			{
				m_pLogdlg = new CChannelLog;
				m_pLogdlg->Create(IDD_CHL_LOG, this);
			}

			m_pLogdlg->SetDefaultChl(chl.unit_id,chl.chl_id);
			m_pLogdlg->SetChlLog(stLog);
			m_pLogdlg->ShowWindow(SW_SHOW);
			return;
		}
		else
		{
			AfxMessageBox(_T("查询通道日志失败"));
			if (0 != g_CommHandler.GetDBHandle().GetDbConnectState())
				TipErr(40001);
		}
	} 

	theApp.m_OpLog.WriteSysOperateLog("查看通道日志", sArg, "失败");
}

void CBatteryViewPage::FreshChlLog(int nDevID,int nChlID)
{
	if (m_pLogdlg && m_pLogdlg->IsWindowVisible() && 
		m_pLogdlg->GetUnitChlNo() != g_BtsMgr.GetUnitChlNo(nDevID,nChlID))
	{
		CGetChlInfoRsp rsp;
		UnitChlList devChls;
		devChls.unit_no = nDevID;
		devChls.vChls.push_back(nChlID);

		string sArg = ToString<short>(nDevID) + "-" + ToString<short>(nChlID);
		if (0 != g_CommHandler.GetClientNet().GetChlInfo(devChls, rsp))
		{
			theApp.m_OpLog.WriteSysOperateLog("查看通道日志", sArg, "获取通道信息失败");
			return;
		}

		if (0 == rsp.m_nRspCode)
		{
			BTS_DATA_CHL chl;
			chl.unit_id = nDevID;
			chl.chl_id = nChlID;
			chl.btDevType = g_BtsMgr.GetUnitItem(chl.unit_id)->GetDevType();
			stChlLogData stLog;
			if (0 < g_CommHandler.GetDBHandle().GetChlHisLog(stLog, rsp.m_infoList[0].m_chlInfo))	
			{
				theApp.m_OpLog.WriteSysOperateLog(" 查看通道日志", sArg, "成功");

				m_pLogdlg->SetDefaultChl(chl.unit_id,chl.chl_id);
				m_pLogdlg->SetChlLog(stLog);
				m_pLogdlg->ShowWindow(SW_SHOW);
				return;
			}
			else if (0 != g_CommHandler.GetDBHandle().GetDbConnectState())
				TipErr(40001);
		} 

		theApp.m_OpLog.WriteSysOperateLog("查看通道日志", sArg, "失败");
	}
}
//右键菜单： 打开数据查看。
void CBatteryViewPage::OnMenuOpenData(void)
{
	//
	CString str;
	//long t1=GetTickCount();//程序段开始前取得系统运行时间(ms)            
	TRACE("CBatteryViewPage::OnMenuOpenData() \r\n");
	if (!m_pCurrentSelect)
	{
		AfxMessageBox(_T("请选择通道"));
		return;
	}
	CGetChlInfoRsp rsp;
	UnitChlList devChls;
	devChls.unit_no = m_pCurrentSelect->GetUnitID();
	devChls.vChls.push_back(m_pCurrentSelect->GetChlID());

	if (0 != g_CommHandler.GetClientNet().GetChlInfo(devChls, rsp))
	{
		AfxMessageBox(_T("获取通道信息失败"));
		return;
	}

	//
	string sArg = ToString<short>(m_pCurrentSelect->GetUnitID()) + "-" + ToString<short>(m_pCurrentSelect->GetChlID());
	CRLog(E_DEBUG,"右键查看通道数据%s",sArg.c_str());
	if (0 == rsp.m_nRspCode)
	{
		BTS_DATA_CHL chl;
		chl.unit_id = m_pCurrentSelect->GetUnitID();
		chl.chl_id = m_pCurrentSelect->GetChlID();
		chl.btDevType = g_BtsMgr.GetUnitItem(chl.unit_id)->GetDevType();
		int nRet = g_CommHandler.OpenChlData(chl, rsp.m_infoList[0].m_chlInfo);
		CRLog(E_DEBUG, "打开数据：guidPath %s", rsp.m_infoList[0].m_chlInfo.guidPath);
		if (0 == nRet)	
		{
			theApp.m_OpLog.WriteSysOperateLog(" 打开通道数据", sArg, "成功");
			return;
		}
		else if (0 != g_CommHandler.GetDBHandle().GetDbConnectState())
			TipErr(40001);
		else
			AfxMessageBox(_T("暂无数据"));
	} 
	theApp.m_OpLog.WriteSysOperateLog("打开通道数据", sArg, "失败");
}


CBatteryControl * CBatteryViewPage::GetBatteryControl()
{
	return  m_pBatteryControl;
}

BOOL CBatteryViewPage::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	OnMouseWheelScroll(zDelta);
	return TRUE;
}

LRESULT CBatteryViewPage::OnUserMsgUpdateBattView(WPARAM wp, LPARAM lp)
{
	//
	TRACE("CBatteryViewPage::OnUserMsgUpdateBattView()\r\n");
	//显示一个串口里所有设备。
	int nDevId = (int)wp;
	int nType = (int)lp;
	bool bDel = false;
	CString strTip;

	if (nType < 0)
	{
		m_pBatteryControl->DeleteUnitItem(nDevId );
		bDel = true;
	}
	else
	{
		if (theApp.m_BattCfg.IsShowAllDev())
		{
			m_pBatteryControl->AddShowDev(nDevId );
		}
		else//显示当前选中设备的电池
		{
			if (nType >= 0)
			{
				m_pBatteryControl->ShowDev(nDevId );
				g_BtsMgr.SetActiveDevID(nDevId);
				CMainFrame::m_netThread.FreshDeviceData(nDevId);

				//提示使用剩余天数
				PriviInfo pri = g_BtsMgr.GetActiveDevPrivi();
				if (pri.m_nLeftDays <= 15 && pri.m_nLeftDays > 0)
				{
					bool bTip = true;
					for (map<int, int>::iterator it = m_mapTipLimitDays.begin(); it != m_mapTipLimitDays.end();it++)
					{
						if (it->second == pri.m_nLeftDays)
						{
							bTip = false;
							break;
						} 
					}	

					if (bTip)
					{
						strTip.Format(_T("还剩余%d天可使用"), pri.m_nLeftDays);
						m_mapTipLimitDays[pri.m_nDev] = pri.m_nLeftDays;
					} 
				} 
			} 
		}
	}

	m_pBatteryControl->UpdateAllBatteryRect();
	m_pBatteryControl->RedrawAllBattery();
	UpdateScrollBar();
	//if (bDel)
	{
		Invalidate();
	}

	if (!strTip.IsEmpty())
	{
		AfxMessageBox(strTip);
	} 
	return 0;
}

LRESULT CBatteryViewPage::OnUserMsgUpdateBattView2(WPARAM wp, LPARAM lp)
{
	m_pBatteryControl->UpdateAllBatteryRect();
	ChangeScrollBar();

	//m_pBatteryControl->RedrawAllBattery();
	Invalidate();
	return 0;
}

void CBatteryViewPage::OnDestroy()
{
	CPropertyPage::OnDestroy();
	TRACE("CBatteryViewPage::OnDestroy()\r\n");
	KillTimer(TIMER_UPDATERT);
}



BOOL CBatteryViewPage::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	// TODO: Add your message handler code here and/or call default
	BOOL bRes = CPropertyPage::OnSetCursor(pWnd, nHitTest, message);
 	HCURSOR myCursor ;
 	if (m_bMouseDraging && m_vecDragBattery.size() != 0)
 	{
		//增加
 		myCursor = AfxGetApp()->LoadCursor(IDC_ARROW);   
 		SetCursor(myCursor);	
 
 	}
	//return CPropertyPage::OnSetCursor(pWnd, nHitTest, message);
	return bRes;
}


void CBatteryViewPage::ChangeScrollBar(/*CRect* rc*/)
{
	SetSBScrollRect();
	CorrectScroll();
	CRect rc;
	GetClientRect(rc);
	//当窗口改变时,滚动条随之移动,滚动条的信息也及时更新
	//CRect hrect;
	//hrect.left = 0;
	//hrect.top = rc.bottom - 17;
	//hrect.right = rc.right ;
	//hrect.bottom = rc.bottom;

	//CRect vrect;
	//vrect.left = rc.right - 17;
	//vrect.top = 0;
	//vrect.right = rc.right;
	//vrect.bottom = rc.bottom;
	//rc.DeflateRect( 0,0,20,20);

	//	return;//后面代码为什么影响定制界面显示
	m_pBatteryControl->SetWndArea(rc);
	UpdateScrollBar();
}

LRESULT CBatteryViewPage::WindowProc( UINT message, WPARAM wParam, LPARAM lParam )
{
	if (message == WM_MOUSEWHEEL)
	{
		MouseWheelMessageDispose(WM_MOUSEWHEEL, wParam, lParam, SB_VERT);
	}
	else if(message == WM_VSCROLL)
	{
		BOOL bPutRedraw = ScrollBarMessageDispose(WM_VSCROLL, wParam, lParam);
		if (bPutRedraw)
		{
			int nXPutPixel = 0, nYPutPixel = 0;
			GetPosPixel(SB_VERT, m_nYUnitPixel, nYPutPixel);
			ScrollWindow(0, -nYPutPixel - m_pBatteryControl->GetOriginPoint().y);
			m_pBatteryControl->MoveOriginPoint(0, -nYPutPixel);
		}
	}
	return CPropertyPage::WindowProc(message, wParam, lParam);
}
//CDrawScrollBar * CBatteryViewPage::GetScrollBar(UINT nBar)
//{
//	return (nBar == SB_HORZ) ? ASSERT(FALSE), NULL : &m_sbV; 
//}
void CBatteryViewPage::MouseWheelMessageDispose( UINT uMsg, WPARAM wParam, LPARAM lParam, UINT nBar )
{
	if (uMsg == WM_MOUSEWHEEL)
	{
		//-- 鼠标滚动值 +120表示向前\左滚动 -120表示向后\右滚动。滚动大小是滚动条3个刻度值
		short zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
		//-- 按键按下标志
		short nFlags = GET_NCHITTEST_WPARAM(wParam);
		/*
		MK_CONTROL // Ctrl
		MK_LBUTTON // 鼠标左键
		MK_MBUTTON // 鼠标中间
		MK_RBUTTON // 鼠标右键
		MK_SHIFT // Shift
		MK_XBUTTON1
		MK_XBUTTON2
		*/
		//-- 获取鼠标当前位置
		int xPos = LOWORD(lParam); 
		int yPos = HIWORD(lParam);

		OnMouseWheelScroll(zDelta);
	}
}

void CBatteryViewPage::OnMouseWheelScroll( short zDelta )
{
	//-- 处理滚动条滚动
	UINT uSbMsg;
	int nPos;
	if (zDelta > 0)
	{
		uSbMsg = SB_LINEUP;
		nPos = -3;
	}
	else
	{
		uSbMsg = SB_LINEDOWN;
		nPos = 3;
	}
	SCROLLINFO si = { sizeof(si) }; 
	si.fMask = SIF_ALL; 
	//CDrawScrollBar * pSB = GetScrollBar(nBar);
	
	if (m_sbV.GetScrollInfo(&si))
	{
		if ((si.nPos == si.nMin && nPos < 0) ||
			(si.nPos == si.nMax - (int)si.nPage + 1 && nPos > 0)
			)
			return;

		nPos += si.nPos;
		if (nPos < si.nMin)
			nPos = si.nMin;
		if (nPos > si.nMax - (int)si.nPage + 1)
			nPos = si.nMax - (int)si.nPage + 1;
		//UINT uSBMsg = (nBar == SB_VERT ? WM_VSCROLL : WM_HSCROLL);
		::SendMessage(GetSafeHwnd(), WM_VSCROLL, MAKEWPARAM(SB_THUMBTRACK, nPos), 0);
	}
}

BOOL CBatteryViewPage::ScrollBarMessageDispose( UINT uSBMsg, WPARAM wParam, LPARAM lParam )
{
	BOOL bPutRedraw = FALSE; 
	if (uSBMsg == WM_HSCROLL || uSBMsg == WM_VSCROLL)
	{ 
		int nSBCode = (int)LOWORD(wParam); 
		int nPos = (int)HIWORD(wParam); 
		//CDrawScrollBar * pSB = (uSBMsg == WM_HSCROLL) ? GetScrollBar(SB_HORZ) : GetScrollBar(SB_VERT); 
		SCROLLINFO si = { sizeof(si) }; 
		si.fMask = SIF_ALL; 
		if (m_sbV.GetScrollInfo(&si))
		{ 
			int nOldPos = si.nPos; 
			switch (nSBCode)
			{
			case SB_TOP :
				si.nPos = si.nMin; break;
			case SB_BOTTOM :
				si.nPos = si.nMax; break;
			case SB_LINEUP : 
				si.nPos --; break;
			case SB_LINEDOWN : 
				si.nPos ++; break;
			case SB_PAGEUP : 
				si.nPos -= si.nPage; break;
			case SB_PAGEDOWN :
				si.nPos += si.nPage; break;
			case SB_THUMBTRACK :
				si.nTrackPos = nPos;
				si.nPos = si.nTrackPos; break;
			default: break;
			}
			if (si.nPos < si.nMin) 
				si.nPos = si.nMin; 
			if (si.nPos > si.nMax - (int)si.nPage + 1) 
				si.nPos = si.nMax - si.nPage + 1; 
			if (si.nPos != nOldPos)
			{ 
				m_sbV.SetScrollInfo(&si); 
				bPutRedraw = TRUE; 
			}
		}
	}
	return bPutRedraw;
}

void CBatteryViewPage::CorrectScroll()
{
	CRect rect;
	GetClientRect(rect);
	if (rect.IsRectEmpty())
		return;

	CSize szDoc = m_pBatteryControl->GetDocSize();
	if (szDoc.cx <= 0 || szDoc.cy <= 0)
		return;

	CPoint ptOrigin = m_pBatteryControl->GetOriginPoint();
	if (ptOrigin.y < 0 &&
		rect.Height() - ptOrigin.y >= szDoc.cy + m_nYUnitPixel)
	{ // 客户区高度在增大
		int nYOffset = rect.Height() - ptOrigin.y - szDoc.cy;
		if (ptOrigin.y + nYOffset > 0)
			nYOffset = -ptOrigin.y;
		m_pBatteryControl->MoveOriginPoint(0, nYOffset);
		ScrollWindow(0, nYOffset);
	}
}

void CBatteryViewPage::UpdateScrollBar()
{
	CRect rcClient;
	GetClientRect(rcClient);
	CSize szDoc = m_pBatteryControl->GetDocSize();;

	BOOL bVertShow = m_sbV.IsShow();
	BOOL btVertShow = FALSE;
	int nVertWidth = m_nsbWidth;
	btVertShow = (szDoc.cy > rcClient.Height());

	if (btVertShow != bVertShow)
	{
		rcClient.right += (btVertShow ? -nVertWidth : nVertWidth);
		//ShowScrollBar(SB_VERT, btVertShow);
		m_sbV.SetShow(btVertShow);
		RecalcLayout();
	}
	//GetClientRect(rcClient);

	CPoint ptOrigin = m_pBatteryControl->GetOriginPoint();
	int nXDocLength = szDoc.cx;
	if (rcClient.Width() - ptOrigin.x > nXDocLength)
		nXDocLength = rcClient.Width() - ptOrigin.x;

	int nYDocLength = szDoc.cy;
	if (rcClient.Height() - ptOrigin.y > nYDocLength)
		nYDocLength = rcClient.Height() - ptOrigin.y;

	InitScrollBarInfo(SB_VERT, rcClient.Height(), nYDocLength, -ptOrigin.y, m_nYUnitPixel);
}

BOOL CBatteryViewPage::RecalcLayout()
{
	m_pBatteryControl->MoveOriginPoint(0, 0);
	return SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|SWP_FRAMECHANGED);
}

int CBatteryViewPage::GetPixelPos( int nPixel, int nUnitPixel )
{
	return (nPixel / nUnitPixel + (nPixel % nUnitPixel ? 1 : 0));
}

void CBatteryViewPage::GetPosPixel( int nBar, int nUnitPixel, int & nPutPixel )
{
	nPutPixel = 0;
	SCROLLINFO si = { sizeof(si) }; 
	si.fMask = SIF_POS;
	//CDrawScrollBar * pSB = GetScrollBar(nBar);
	if (m_sbV.GetScrollInfo(&si)) 
		nPutPixel = si.nPos * nUnitPixel; 
	else 
		nPutPixel = 0;
}

void CBatteryViewPage::InitScrollBarInfo( UINT nBar, int nClientLength, int nDocLength, int nCurPixel, int nUnitPixel )
{
	assert(nBar != SB_CTL);
	if (::IsWindow(GetSafeHwnd())) 
	{ 
		SCROLLINFO si = { sizeof(SCROLLINFO) }; 
		si.fMask = SIF_ALL; 
		//CDrawScrollBar * pSB = GetScrollBar(nBar);
		if (m_sbV.GetScrollInfo(&si)) 
		{ 
			if (nClientLength >= nDocLength)
			{
				si.nPage = 0; 
				si.nMin = 0; 
				si.nMax = 0;
				si.nPos = 0;
				si.nTrackPos = 0;
			}
			else
			{
				si.nPage = nClientLength / nUnitPixel; 
				si.nMin = 0; 
				si.nMax = ((nDocLength - nClientLength) / nUnitPixel) + 
					(((nDocLength - nClientLength) % nUnitPixel) ? 1 : 0) - 1 - si.nMin + si.nPage; 
			}

			// 计算当前nPos
			if (nCurPixel >= 0)
			{
				int nPos = GetPixelPos(nCurPixel, nUnitPixel);
				si.nPos = nPos;
			}
			m_sbV.SetScrollInfo(&si); 
		}
	}
}

void CBatteryViewPage::InitScrollBar()
{
	m_sbV.SetTypeStyle(CDrawScrollBar::eSVert);
	m_sbV.SetShowStyle(CDrawScrollBar::eSShowFrame);
	m_sbV.SetOwnerWnd(GetSafeHwnd(), GetSafeHwnd());
}

void CBatteryViewPage::OnNcCalcSize( BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp )
{
	if (m_sbV.IsShow())
		lpncsp->rgrc[0].right -= m_nsbWidth;
	CPropertyPage::OnNcCalcSize(bCalcValidRects, lpncsp);
}

void CBatteryViewPage::SetSBScrollRect()
{
	if (IsWindow(GetSafeHwnd()))
	{
		CRect rcWindow;
		GetWindowRect(rcWindow);
		CRect rcClient;
		GetClientRect(rcClient);
		MapWindowPoints(CWnd::GetDesktopWindow(), rcClient);
		rcClient.OffsetRect(-rcWindow.TopLeft());

		if (m_sbV.IsShow())
		{
			CRect rcVSB(rcClient.right, rcClient.top, rcClient.right + m_nsbWidth, rcClient.bottom);
			m_sbV.Move(rcVSB);
		}
		//if (m_sbH.IsShow())
		//{
		//    CRect rcHSB(rcClient.left, rcClient.bottom, rcClient.right, rcClient.bottom + m_nsbWidth);
		//    m_sbH.Move(rcHSB);
		//}
	}
}
//
LRESULT CBatteryViewPage::OnMenuCommand( WPARAM wParam, LPARAM lParam )
{
	CMenuItem * pMenuItem = (CMenuItem *)wParam;
	if (pMenuItem)
	{
		switch (pMenuItem->GetID())
		{
		case MENU_RB_BATT_START:
			{
				OnMenuStart();
			}
			break;
		case MENU_RB_BATT_STOP:
			{
				if (IDOK == AfxMessageBox(_T("确定停止吗?"), MB_OKCANCEL))
				{
					OnSimpleControl(CLS_MID_CMD_STOP_CHL_TEST);
				}
			}
			break;
		case MENU_RB_BATT_PAUSE:
			{
				OnSimpleControl(CLS_MID_CMD_PAUSE_CHL_TEST);
			}
			break;
		case MENU_RB_BATT_CONTINUE:
			{
				OnSimpleControl(CLS_MID_CMD_COUTINUE_CHL_TEST);
			}
			break;
		case MENU_RB_BATT_JUMP:
			{
				OnMJump();
			}
			break;
		case MENU_RB_BATT_CLEAR:
			{
				OnSimpleControl(CLS_MID_CMD_CLEAR_CHL_TEST);			
			}
			break;
		case MENU_RB_BATT_CHL_INFO:
			{
				OnMInfo();
			}
			break;
		case MENU_RB_BATT_CHL_DATA:
			{
				OnMenuOpenData();
			}
			break;
		case MENU_RB_BATT_CHL_LOG:
			{
				OnMenuChlLog();
			}
			break;
		case MENU_RB_BATT_SEL_ALL:
			{
				OnMSelectAll();
			}
			break;
		case MENU_RB_BATT_COPY:
			{
				OnMenuCopy();
			}
			break;
		case MENU_RB_BATT_COPYSTART:
			{
				OnMenuCopyStart();
			}
			break;
		case MENU_RB_BATT_2PARALELL:
			{
				SetParallel(2);
			}
			break;
		case MENU_RB_BATT_1PARALELL:
			{
				SetParallel(1);
			}
			break;
		case MENU_RB_BATT_4PARALELL:
			{
				SetParallel(4);
			}
			break;
		case MENU_RB_BATT_8PARALELL:
			{
				SetParallel(8);
			}
			break;
		case MENU_RB_BARCODE_START:
			OnMenuBarStart();
			break;
		}

	}
	return 1;
}

void CBatteryViewPage::OnSimpleControl( const WORD wCmd )
{
	vector<UnitChlList> vChlSel = m_pBatteryControl->GetSelectChl();
	for (size_t i = 0; i < vChlSel.size(); i++)
	{
		int nRet = g_CommHandler.GetClientNet().ChangeChlTest(vChlSel[i], wCmd);
		theApp.m_OpLog.OnControlChannel(wCmd, vChlSel[i], nRet);
	}	
}

BOOL CBatteryViewPage::PreTranslateMessage( MSG* pMsg )
{
	if (pMsg->message == WM_KEYDOWN)
	{
		if (pMsg->wParam == VK_RETURN)
			return FALSE;

		if (g_enterInput)
		{
			g_enterInput = FALSE;
			SetTimer(1, 300, NULL);
		} 

		int num = pMsg->wParam;
		if (num>=48 && num<=57)
		{
			g_inputStr += '0'+num-48;
		}
		else if (num >= 'A' && num <= 'Z')
		{
			g_inputStr += 'A'+num-(int)'A';
		} 
		else if (num == VK_SPACE)
			g_inputStr += ' ';
	} 

	return CPropertyPage::PreTranslateMessage(pMsg);
}


//


BOOL CBatteryViewPage::OnSetActive()
{
	// TODO: 在此添加专用代码和/或调用基类
	CMainFrame *pMainFrame = (CMainFrame *)GetParentFrame();
	pMainFrame->m_wndSplitter.SetRowInfo(0,40,40);
	if(m_isFist > 3)  //软件启动时会调用几次，这几次会引起崩溃。
		pMainFrame->m_wndSplitter.RecalcLayout();
	else
		m_isFist++;
	return CPropertyPage::OnSetActive();
}

void CBatteryViewPage::SetParallel( int nType )
{
	vector<UnitChlList> vChlSel = m_pBatteryControl->GetSelectChl();
	
	for (size_t i = 0; i < vChlSel.size(); i++)
	{
		CUnitItem* pUnit = g_BtsMgr.GetUnitItem(m_pCurrentSelect->GetUnitID());
		if (!pUnit)
			continue;

		bool checkChlState = true;
		string sArg;
		
		vector<UnitChlList> vPallel;
		if (1 == nType)
		{
			map<int,int> mapSet;
			for (int c = 0; c < vChlSel[i].vChls.size(); ++c)
			{
				CBatteryItem* pBat = pUnit->GetChl(vChlSel[i].vChls[c]);
				if (CBatteryItem::BTS_WORKTYPE_INPROESS == pBat->GetWorkType())	
				{
					checkChlState = false;
					break;
				}
				
				vector<short>	vChilds = pBat->GetChilds();	//拆开并联，通道展开
				sArg += ToString<int>(pBat->GetUnitID())+"-"+ToString<int>(pBat->GetChlID())+"("+ToString<int>(vChilds.size())+");";
				
				for (size_t k = 0; k < vChilds.size(); ++k)
				{
					UnitChlList devChls;
					devChls.unit_no = vChilds[k];
					if (mapSet.find(vChilds[k]) != mapSet.end())
					{
						CRLog(E_ERROR,"重复并联解除通道号%d", vChilds[k]);
						continue;
					}
					devChls.vChls.push_back(vChilds[k]);
					vPallel.push_back(devChls);
					mapSet[vChilds[k]] = vChilds[k];
				}
			}
		}
		else if (2 == nType || 4 == nType || 8 == nType)
		{
			short cChlTmp = 0,tts = 0;
			int nInf =0;
			for (int j = 0;j < vChlSel[i].vChls.size(); ++j)
			{
				int c = vChlSel[i].vChls[j];
				if((int)(c-1)/nType == (int)tts/nType && j != 0)
					continue;
				for(int k = 0;k < nType;k++)
				{
					if(c%nType == 1)
						break;
					else
						c--;
				}
				cChlTmp = tts = c;

				CBatteryItem* pBat = pUnit->GetChl(cChlTmp);
				int nChl = pBat->GetChilds().size();
				int nNum = nChl/nType>1?nChl/nType:1; 
				
				for(int i = 0;i<nNum;i++)
				{
					UnitChlList devChls;
					devChls.unit_no = cChlTmp + i * nType;	//主通道号
;
					for(int j = 0;j < nType;j++)
					{
						if (CBatteryItem::BTS_WORKTYPE_INPROESS == pUnit->GetChl(devChls.unit_no + j)->GetWorkType())
						{
							checkChlState = false;
							break;
						}

						devChls.vChls.push_back(devChls.unit_no + j);
					}

					vPallel.push_back(devChls);
				}
			}
		}

		if (!checkChlState)
		{
			AfxMessageBox(_T("运行中的通道不能执行并联设置"));
			return;
		} 
		
		if (vPallel.size() > 0)

		{
			SetParallelReq req;
			req.m_nCount = vPallel.size();
			ASSERT(req.m_nCount < Check_Max_Chl);
			req.m_paInfo = new ParallelInfo[req.m_nCount];
			for (size_t k = 0; k < req.m_nCount; ++k)
			{
				req.m_paInfo[k].chlNo = vPallel[k].unit_no;
				req.m_paInfo[k].chlInfo.nChlCount = vPallel[k].vChls.size();
				ASSERT(vPallel[k].vChls.size() < Check_Max_Chl);
				req.m_paInfo[k].chlInfo.pchlNo = new short[vPallel[k].vChls.size()];
				std::copy(vPallel[k].vChls.begin(), vPallel[k].vChls.end(), req.m_paInfo[k].chlInfo.pchlNo);
			}
			DWORD t1 = GetTickCount();
			int nRet = g_CommHandler.GetClientNet().SetParallelInfo(vChlSel[i].unit_no,req);
			DWORD tErr = GetTickCount() - t1;
			CRLog(E_DEBUG, "并联个数%d, %d, 消耗%d", req.m_nCount,nRet, tErr);
			string strLog;
			string strLog2;
			for(int j = 0;j < req.m_nCount;j++)
			{
				strLog += std::to_string((_Longlong)req.m_paInfo[j].chlNo) + ',';
				for(int k = 0;k < req.m_paInfo[j].chlInfo.nChlCount;k++)
				{
					strLog2 += std::to_string((_Longlong)req.m_paInfo[j].chlInfo.pchlNo[k]) + ',';
				}
				strLog2 += '@';
			}
			CRLog(E_DEBUG, "并联%s",strLog.c_str());
			CRLog(E_DEBUG, "子通道%s",strLog2.c_str());
			if (0 == nRet)
			{	
				AfxMessageBox(_T("并联设置成功"));
				g_CommHandler.GetClientNet().FreshParallelInfo(vChlSel[i].unit_no);
				theApp.GetMainWnd()->PostMessage(CBtsControlApp::USER_MSG_UPDATE_BATTVIEW2);
			}
			else
				AfxMessageBox(_T("并联设置失败"));


			theApp.m_OpLog.WriteSysOperateLog(ToString<int>(nType)+"通道并联设置", sArg, theApp.GetClientTipMsg(nRet));

			//if (tErr > 4000)
			//{
			//	AfxMessageBox(_T("请重启客户端"));
			//	theApp.m_OpLog.WriteSysOperateLog(ToString<int>(nType)+"通道并联设置", sArg, "未知");
			//	return ;
			//}
		}
	}
}

void CBatteryViewPage::OnMenuBarStart( void )
{
	UnitChlList mapUnitChls = GetSelectChls();
	if (mapUnitChls.vChls.size() == 0)
		return;

	CStartChannel dlg;
	dlg.SetStartChlNo(mapUnitChls);
	dlg.DoModal();
}


void CBatteryViewPage::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (!m_pBatteryControl)
	{
		return;
	}
	//
	m_pCurrentSelect = NULL;
	CBatteryItem* pCurrentSelect = m_pBatteryControl->GetBatteryIndexByPoint(point);


	if (!pCurrentSelect)
	{
		//没有有效电池被选中。
		TRACE("CBatteryViewPage::OnRButtonUp() 没有有效电池被选中\r\n");

		return;
	}
	
	PriviInfo pri = g_BtsMgr.GetActiveDevPrivi();
	if (pri.m_nLeftDays <= 0)
	{
		if (pri.m_nPrivi == 0)
		{
			AfxMessageBox(_T("试用期已经结束，请联系供应商."));
			return ;
		} 
		else if (pri.m_nPrivi == 1)
		{
			AfxMessageBox(_T("第一期款使用期已经结束，请联系供应商."));
			return ;
		} 
		else if (pri.m_nPrivi == 2)
		{
			AfxMessageBox(_T("第二期款使用期已经结束，请联系供应商."));
			return ;
		} 
	} 
	m_pCurrentSelect = pCurrentSelect;
	OnMenuOpenData();

	CPropertyPage::OnLButtonDblClk(nFlags, point);
}

UnitChlList CBatteryViewPage::GetSelectChls()
{
	UnitChlList unitChls;
	CUnitItem* pUnit = g_BtsMgr.GetActiveDev();
	if (!pUnit)
		return unitChls;

	unitChls.unit_no = pUnit->GetUnitNo();
	vector< CBatteryItem* > vBat = pUnit->GetAllBattery();
	vector< CBatteryItem* >::iterator it = vBat.begin();
	while (it != vBat.end())
	{
		if ((*it)->GetStatus() && (*it)->GetBatteryShow())
		{
			unitChls.vChls.push_back((*it)->GetChlID());
#ifndef _DEBUG
			CBatteryItem* pBat = pUnit->GetChl((*it)->GetChlID());
			if (pBat->GetWorkType() != CBatteryItem::BTS_WORKTYPE_FINISH && 
				pBat->GetWorkType() != CBatteryItem::BTS_WORKTYPE_USERSTOP)
			{
				CString str;
				str.Format(_T("%d-%d 不能直接启动,请先停止"), pUnit->GetUnitNo(), (*it)->GetChlID());
				AfxMessageBox(str);
				unitChls.vChls.clear();
				return unitChls;
			}
#endif
		}
		it++;
	}

	return unitChls;
}

void CBatteryViewPage::TipErr( int nErrCode )
{
	CMainFrame *pMainFrame = (CMainFrame *)GetParentFrame();
	pMainFrame->PostMessage(CBtsControlApp::USER_MSG_SOFT_TIP, nErrCode, 0);
}
